<?php
require_once( ABSPATH . 'wp-load.php' );
require_once 'common-model.php';


class CategoryModel{
    private $table = "ys_category";
    
    public static function insertNewCategory($data) {
        global $wpdb;
        try {
            return $wpdb->insert( $this->table,  $data, $format);
        } catch (Exception $e) {
            //log here
            return false;
        }
    }

    public static function deleteCategory($condition) {
        global $wpdb;
        try {
            return $wpdb->delete($this->table, $condition, null);
        } catch (Exception $e) {
            //log here
            return false;
        }
    }

    public static function updateCategory($data, $format = null, $where_format = null) {
        global $wpdb;
        try {
            return $wpdb->update($this->table, $data, $where, $format, $where_format);
        } catch (Exception $e) {
            //log here
            return false;
        }
    }
    
    /**
     *  $wpdb->get_col( 'query', column_offset ); 
     *  $wpdb->get_results( 'query', output_type );
     *  $wpdb->get_row('query', output_type, row_offset);
     *  $wpdb->get_var( 'query', column_offset, row_offset );
     * 
     * @param $query : câu lệnh query
     * @param $data1 : get_row - output_type : OBJECT, ARRAY_A,ARRAY_N
     *                 get_col - column_offset 
     *                 get_generic -output_type : OBJECT, OBJECT_K, ARRAY_A,ARRAY_N
     *                 get_variable - column_offset 
     */
    public static function selectCategory($query, $data1 , $data2 = NULL, $selectType = CommonModel::SELECT_TYPE_RESULTS) {
        global $wpdb;
        try {
            if($selectType == CommonModel::SELECT_TYPE_COLUMN){
                return $wpdb->get_col($query, $data1);
            }
           
            if($selectType == CommonModel::SELECT_TYPE_RESULTS){
                return $wpdb->get_results($query, $data1);
            }
            if($selectType == CommonModel::SELECT_TYPE_VARIABLE){
                return $wpdb->get_var($query, $data1, $data2 );
            }
            
            if($selectType == CommonModel::SELECT_TYPE_ROW){
                return $wpdb->get_row($query, $data1, $data2);
            }
        } catch (Exception $e) {
            //log here
            return false;
        }
    }
}
?>
